package com.zhixue.media.controller;

import com.zhixue.media.dto.UploadFileParamsDto;
import com.zhixue.media.service.MediaFilesService;
import com.zhixue.model.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: 李 力
 * @Date: 2025/4/27
 * @Description: com.zhixue.media.controller
 * @version: 1.0
 */
@RestController
@RequestMapping
public class BigFileController {

    @Autowired
    private MediaFilesService mediaFilesService;

    //文件上传前检查文件
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile(@RequestParam("fileMd5") String fileMd5) {
        return mediaFilesService.checkFile(fileMd5);
    }

    //分块文件上传前的检测
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") Integer chunk) {
        return mediaFilesService.checkChunk(fileMd5, chunk);
    }


    //上传分块文件
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadChunk(@RequestPart("file") MultipartFile file,
                                             @RequestParam("fileMd5") String fileMd5,
                                             @RequestParam("chunk") Integer chunk) {

        return mediaFilesService.uploadChunk(file, fileMd5, chunk);
    }

    //合并文件
    @PostMapping("/upload/mergechunks")
    public RestResponse mergeChunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) {

        //todo 机构ID暂时写死
        Long companyId = 1L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setTags("视频文件");
        uploadFileParamsDto.setFileType("001002");
        return mediaFilesService.mergeChunks(companyId, fileMd5, chunkTotal, uploadFileParamsDto);
    }
}
